package org.vaadin.nf4v.ext.config.secure;

import org.vaadin.nf4v.core.NavigableApplication;
import org.vaadin.nf4v.core.config.AbstractConfig;
import org.vaadin.nf4v.core.navigation.NavigationManager;
import org.vaadin.nf4v.core.navigation.NavigationRequest;

/**
 * Utility class.
 * Provides access to windows and pages security configuration.
 */
public class ExtSecureUtil {

    /**
     * @param config configuration
     * @return array of required roles
     */
    public static String[] getRequiredRoles(AbstractConfig config) {
        return config.getAttribute(SecureAnnotationParser.ROLES);
    }

    /**
     * @param config configuration
     * @return array of denied roles
     */
    public static String[] getDeniedRoles(AbstractConfig config) {
        return config.getAttribute(SecureAnnotationParser.ROLES_DENY);
    }

    /**
     * @param config configuration
     * @return array of required permissions
     */
    public static String[] getRequiredPermissions(AbstractConfig config) {
        return config.getAttribute(SecureAnnotationParser.PERMISSIONS);
    }

    /**
     * Shortcut method to use from NavigationListener.
     *
     * @param request navigation request
     * @return array of required roles
     */
    public static String[] getPageRequiredRoles(NavigationRequest request) {
        AbstractConfig config = NavigableApplication.getCurrent()
                .getNavigationConfig().getPageConfig(NavigationManager.getNavigationContext(), request.getTarget().getPageClass());
        return getRequiredRoles(config);
    }

    /**
     * Shortcut method to use from NavigationListener.
     *
     * @param request navigation request
     * @return array of denied roles
     */
    public static String[] getPageDeniedRoles(NavigationRequest request) {
        AbstractConfig config = NavigableApplication.getCurrent()
                .getNavigationConfig().getPageConfig(NavigationManager.getNavigationContext(), request.getTarget().getPageClass());
        return getDeniedRoles(config);
    }

    /**
     * Shortcut method to use from NavigationListener.
     *
     * @param request navigation request
     * @return array of required permissions
     */
    public static String[] getPageRequiredPermissions(NavigationRequest request) {
        AbstractConfig config = NavigableApplication.getCurrent()
                .getNavigationConfig().getPageConfig(NavigationManager.getNavigationContext(), request.getTarget().getPageClass());
        return getRequiredPermissions(config);
    }
}
